﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Microsoft.Practices.Unity;

namespace GNSCore.Services.UnityAdapters
{
    public class UnityExtension : IExtension<ServiceHostBase>, IDisposable
    {
        private static readonly object containerLock = new object();
        private static IUnityContainer containerSingleton;

        internal UnityExtension(IUnityContainer container)
        {
            lock (containerLock)
            {
                if (containerSingleton == null)
                {
                    containerSingleton = container;
                }
            }
        }

        internal IUnityContainer Container
        {
            get { return containerSingleton; }
        }

        #region IExtension<ServiceHostBase> Members
        public void Attach(ServiceHostBase owner)
        {
            // Not used.
        }

        public void Detach(ServiceHostBase owner)
        {
            // Not used.
        }
        #endregion

        #region IDisposable Members
        public void Dispose()
        {
            lock (containerLock)
            {
                if (containerSingleton == null)
                {
                    containerSingleton.Dispose();
                    containerSingleton = null;
                }
            }
        }
        #endregion
    }
}